#include <tsin/library/DynamicDelay.hpp>

#include <tsin/util/Logger.hpp>

#include <cstring>

using namespace tsin;

DynamicDelay::DynamicDelay()
        : tick(0), delay(0)
{
}

DynamicObject *DynamicDelay::copy()
{
        return DynamicObject::copy(new DynamicDelay);
}

void DynamicDelay::init(int delay, DynamicObject *obj)
{
        this->delay = delay;
        this->obj = obj;
        tick = 1;
}

void DynamicDelay::reinit()
{
        for (int i = 0; i < 2; i++)
                arguments[i]->reinit();

        float arg1 = arguments[0]->getInt();
        DynamicObject *arg2 = arguments[1];

        switch(nrArguments) {
        case 2:
                if (desp[0] == 'N')
                        init(arg1, arg2);
                break;
        }
}

DynamicEvent DynamicDelay::postTick(void *data, void *priv_data)
{
        DynamicEvent ret = EVENT_NONE;
        if (tick >= delay) {
                ret |= obj->postTick(data, priv_data);
                tick = 1;
        } else
                tick ++;
        return ret;
}

char DynamicDelay::getDescriptor()
{
        return obj->getDescriptor();
}

int DynamicDelay::getInt()
{
        return obj->getInt();
}

float DynamicDelay::getFloat()
{
        return obj->getFloat();
}

extern "C" LIBRARY_DLLEXPORT DynamicObject *bindDelay()
{
        return new DynamicDelay;
}

